﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.IO;
using System.Windows.Forms;

namespace weiqijiance.信号处理模块
{
    public class DACold : DACBase
    {
        byte[] b = new byte[8];
        /// <summary>
        /// 打开串口
        /// </summary>
        /// <param name="sSet1">串口设置</param>
        /// <returns>是否成功打开</returns>
        public override bool openCOM(string sSet1)
        {
            commex = new SerialPort();
            bool breturn = base.openCOM(sSet1);
            for (int k = 0; k < 8; k++)//红外
            {
                b[k] = (byte)Math.Pow(2, k);
            }
            commex.ReadTimeout = 500;
            commex.ReceivedBytesThreshold = 52;//达到50字节以后才开始触发串口
            commex.DataReceived += comDAC_DataReceived;
            sendstart();
            return breturn;
        }
        public void sendstart()
        {
            Byte[] m_byte = new Byte[6] { 0xff, 0x0f, 0x00, 0xfe, 0x38, 0x03 };//FF 0F 00 FE 38 03 
            commex.Write(m_byte, 0, 6);
        }
        private List<byte> buffer = new List<byte>(4096);

        private void comDAC_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            int n = commex.BytesToRead;
            if (n > 0)
            {
                byte[] buf = new byte[n];
                commex.Read(buf, 0, n);
                buffer.AddRange(buf);
                while (buffer.Count > 19)
                {
                    if (buffer[0] == 0xFF && buffer[17] == 0xFE)
                    {
                        short uSum = 0;
                        short uSum1 = 0;
                        uSum = (short)(buffer[18] * 256 + buffer[19]);
                        for (int i = 0; i < 18; i++)
                        {
                            uSum1 += buffer[i];
                        }

                        if (uSum1 == uSum)
                        {
                            //模拟
                            for (int i = 0; i < 5; i++)//模拟4路
                            {
                                fAIDataex[i] = ((buffer[i * 2 + 1] & 0x0F) * 256 + buffer[i * 2 + 2]) * 5000 / 4096;
                            }

                            ///////速度
                            for (int j = 0; j < 2; j++)// 
                            {
                                fTIData[j] = buffer[2 * j + 13] * 256 + buffer[2 * j + 14];
                            }

                            ////////红外
                            //if (buffer[11] == 0x66)//红外
                            {
                                for (int k = 0; k < 8; k++)//红外
                                {
                                    bDIData[k] = (buffer[12] & b[k]) == b[k] ? true : false;
                                }
                            }
                            buffer.RemoveRange(0, 20);
                        }
                        else
                        {
                            buffer.RemoveAt(0);
                        }
                    }
                    else //帧头不正确时，记得清除
                    {
                        buffer.RemoveAt(0);
                    }
                }
            }

        }
    }
}
